/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
	"fmt"
	"strconv"
	"strings"

	devices "gitee.com/zinface/gpio-ctl/pkg/gpio"
	"github.com/spf13/cobra"
)

// fastmodeCmd represents the fastmode command
var fastmodeCmd = &cobra.Command{
	Use:   "fastmode",
	Short: _desc_fastmode,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) > 0 {
			id, err := strconv.Atoi(args[0])
			if err != nil {
				panic(err)
			}

			gpio := devices.NewGPIODevice(id)
			// var skipStep bool = false

			for i := 0; i < len(args); i++ {
				if strings.Compare(args[i], "export") == 0 {
					gpio.Export()
					fmt.Println("  export:", id)
					continue
				}

				if strings.Compare(args[i], "unexport") == 0 {
					gpio.UnExport()
					fmt.Println("unexport:", id)
					continue
				}

				// value
				if strings.Compare(args[i], "getv") == 0 ||
					strings.Compare(args[i], "getvalue") == 0 {
					getv, _ := gpio.Value()
					fmt.Println("getvalue:", getv)
				}

				if strings.Compare(args[i], "setv") == 0 ||
					strings.Compare(args[i], "setvalue") == 0 {
					i++
					setv, _ := strconv.Atoi(args[i])
					gpio.ChangeValue(devices.GPIOValue(setv))
					fmt.Println("setvalue:", setv)
				}

				// mode
				if strings.Compare(args[i], "getm") == 0 ||
					strings.Compare(args[i], "getmode") == 0 {
					getm, _ := gpio.Mode()
					fmt.Println(" getmode:", getm)
				}

				if strings.Compare(args[i], "setm") == 0 ||
					strings.Compare(args[i], "setmode") == 0 {
					i++
					var setm int
					if strings.Contains(args[i], "in") {
						setm = int(devices.DIRECTION_IN)
					} else if strings.Contains(args[i], "out") {
						setm = int(devices.DIRECTION_OUT)
					} else {
						setm, err = strconv.Atoi(args[1])
						if err != nil {
							panic(err)
						}
					}
					gpio.ChangeMode(devices.GPIOMode(setm))
					fmt.Println(" setmode:", setm)
				}

				// edge
				if strings.Compare(args[i], "gete") == 0 ||
					strings.Compare(args[i], "getedge") == 0 {
					gete, _ := gpio.Edge()
					fmt.Println(" getedge:", gete)
				}

				if strings.Compare(args[i], "sete") == 0 ||
					strings.Compare(args[i], "setedge") == 0 {
					i++
					var sete devices.GPIOEdge
					if strings.Contains(args[i], "none") {
						sete = devices.EDGE_NONE
					} else if strings.Contains(args[i], "rising") {
						sete = devices.EDGE_RISING
					} else if strings.Contains(args[i], "falling") {
						sete = devices.EDGE_FALLING
					} else if strings.Contains(args[i], "both") {
						sete = devices.EDGE_BOTH
					}
					gpio.ChangeEdge(sete)
					fmt.Println(" setedge:", sete)
				}

				// other
				if strings.Compare(args[i], "info") == 0 {
					gpio := devices.NewGPIODevice(id)
					gexported := gpio.IsExported()
					gedge, _ := gpio.Edge()
					gmode, _ := gpio.Mode()
					gvalue, _ := gpio.Value()

					fmt.Println("Exported:", gexported)
					fmt.Println("    Mode:", gmode)
					fmt.Println("    Edge:", gedge)
					fmt.Println("   Value:", gvalue)
				}
			}
		}
	},
}

func init() {
	rootCmd.AddCommand(fastmodeCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// fastmodeCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// fastmodeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
